<?php

class Silk_Zgb_Adminhtml_AuditController extends Mage_Adminhtml_Controller_Action
{	
	public function indexAction(){
		$this->_title($this->__('Catalog'))
             ->_title($this->__('商品审核'));

        $this->loadLayout();
        $this->renderLayout();
	}	

	public function massStatusAction(){
        $productIds = (array)$this->getRequest()->getParam('product');
        $storeId    = (int)$this->getRequest()->getParam('store', 0);
        $status     = (int)$this->getRequest()->getParam('status');

        try {
            $this->_validateMassStatus($productIds, $status);
            Mage::getSingleton('catalog/product_action')
                ->updateAttributes($productIds, array('status' => $status, 'status_at' => Mage::getModel('core/date')->date()), $storeId);

            Mage::dispatchEvent('catalog_controller_product_mass_status', array('product_ids' => $productIds));

            $this->_getSession()->addSuccess(
                $this->__('Total of %d record(s) have been updated.', count($productIds))
            );
        }
        catch (Mage_Core_Model_Exception $e) {
            $this->_getSession()->addError($e->getMessage());
        } catch (Mage_Core_Exception $e) {
            $this->_getSession()->addError($e->getMessage());
        } catch (Exception $e) {
            $this->_getSession()
                ->addException($e, $this->__('An error occurred while updating the product(s) status.'));
        }

        $this->_redirect('*/*/', array('store'=> $storeId));
		// $productIds = (array)$this->getRequest()->getParam('product');
  //       $storeId    = (int)$this->getRequest()->getParam('store', 0);
  //       $visibility     = (int)$this->getRequest()->getParam('visibility');

  //       try {
  //           $this->_validateMassVisibility($productIds, $visibility);
  //           Mage::getSingleton('catalog/product_action')
  //               ->updateAttributes($productIds, array('visibility' => $visibility), $storeId);
  //           Mage::dispatchEvent('catalog_controller_product_mass_status', array('product_ids' => $productIds));

  //           $this->_getSession()->addSuccess(
  //               $this->__('Total of %d record(s) have been updated.', count($productIds))
  //           );
  //       }
  //       catch (Mage_Core_Model_Exception $e) {
  //           $this->_getSession()->addError($e->getMessage());
  //       } catch (Mage_Core_Exception $e) {
  //           $this->_getSession()->addError($e->getMessage());
  //       } catch (Exception $e) {
  //           $this->_getSession()
  //               ->addException($e, $this->__('An error occurred while updating the product(s) status.'));
  //       }

  //       $this->_redirect('*/*/', array('store'=> $storeId));
	}

    /**
     * Validate batch of products before theirs status will be set
     *
     * @throws Mage_Core_Exception
     * @param  array $productIds
     * @param  int $status
     * @return void
     */
    public function _validateMassStatus(array $productIds, $status)
    {
        if ($status == Mage_Catalog_Model_Product_Status::STATUS_ENABLED) {
            if (!Mage::getModel('catalog/product')->isProductsHasSku($productIds)) {
                throw new Mage_Core_Exception(
                    $this->__('Some of the processed products have no SKU value defined. Please fill it prior to performing operations on these products.')
                );
            }
        }
    }

    /**
     * Product grid for AJAX request
     */
    public function gridAction()
    {
        $this->loadLayout();
        $this->renderLayout();
    }

	/**
     * Validate batch of products before theirs status will be set
     *
     * @throws Mage_Core_Exception
     * @param  array $productIds
     * @param  int $status
     * @return void
     */
    public function _validateMassVisibility(array $productIds, $status)
    {
        // if ($status == Mage_Catalog_Model_Product_Status::STATUS_ENABLED) {
        //     if (!Mage::getModel('catalog/product')->isProductsHasSku($productIds)) {
        //         throw new Mage_Core_Exception(
        //             $this->__('Some of the processed products have no SKU value defined. Please fill it prior to performing operations on these products.')
        //         );
        //     }
        // }
    }
}
